package com.auxgroup.hr.front.commons.utils;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;

public class RetrofitUtil {
	
	private static  HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new RetrofitHttpLogger()).setLevel(HttpLoggingInterceptor.Level.BODY);
	private static OkHttpClient client = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS).
			readTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).addInterceptor(loggingInterceptor).build();
	private static OkHttpClient noLogclient = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS)
			.readTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).build();

	/**
	 * 调用第三方服务
	 * 
	 * @param baseURL
	 * @param service
	 * @return
	 */
	public static <T> T start(String baseURL, Class<T> service) {
		return new Retrofit.Builder().baseUrl(baseURL).client(client).addConverterFactory(GsonConverterFactory.create()).build()
				.create(service);
	}
	
	/**
	 * 调用第三方服务  jackson 格式
	 * 
	 * @param baseURL
	 * @param service
	 * @return
	 */
	public static <T> T jacksonStart(String baseURL, Class<T> service) {
		return new Retrofit.Builder().baseUrl(baseURL).client(client).addConverterFactory(JacksonConverterFactory.create()).build()
				.create(service);
	}
	
	/**
	 * 
	 * @param baseURL
	 * @param service
	 * @return
	 */
	public static <T> T startNotLog(String baseURL, Class<T> service) {
		return new Retrofit.Builder().baseUrl(baseURL).client(noLogclient).addConverterFactory(GsonConverterFactory.create()).build()
				.create(service);
	}

}
